package cn.edu.zust.fileservice.untils;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.*;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Component
public class QiniuKodoUtil {
    Configuration cfg = new Configuration(Region.region0());
    @Value("msTjDlDlEdydw_uPLS-cCi33IXqECLFDIQb9am0x")
    String accessKey;
    @Value("APoYuZ3XSS78C9vu3ovAL4lKLHDhH9FuLYBYb7RL")
    String secretKey;
    @Value("tempk")
    String bucket;
    @Value("swjs28eb6.hd-bkt.clouddn.com")
    String domain;

    //上传图片
    public String uploadPictures(MultipartFile multipartFile) {
        UploadManager uploadManager = new UploadManager(cfg);

        // 获取原始文件名
        String originalFileName = multipartFile.getOriginalFilename();
        // 获取文件后缀名，例如.jpg、.png等，从最后一个.开始截取后面的部分
        String fileExtension = "";
        if (originalFileName!= null && originalFileName.lastIndexOf('.') > 0) {
            fileExtension = originalFileName.substring(originalFileName.lastIndexOf('.'));
        }
        // 生成带时间戳的文件名，格式示例：原文件名_时间戳.后缀名
        String newFileName = System.currentTimeMillis() + "_" + originalFileName;
        if (!fileExtension.isEmpty()) {
            newFileName = newFileName.substring(0, newFileName.lastIndexOf('.')) + fileExtension;
        }
        String key = "pictures/" + newFileName;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(multipartFile.getBytes(), key, upToken);
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
        } catch (QiniuException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "http://swjs28eb6.hd-bkt.clouddn.com/" + key;
    }

    //上传图片
    public String uploadVideos(MultipartFile multipartFile) {
        UploadManager uploadManager = new UploadManager(cfg);
        // 获取原始文件名
        String originalFileName = multipartFile.getOriginalFilename();
        // 获取文件后缀名，例如.jpg、.png等，从最后一个.开始截取后面的部分
        String fileExtension = "";
        if (originalFileName!= null && originalFileName.lastIndexOf('.') > 0) {
            fileExtension = originalFileName.substring(originalFileName.lastIndexOf('.'));
        }
        // 生成带时间戳的文件名，格式示例：原文件名_时间戳.后缀名
        String newFileName = System.currentTimeMillis() + "_" + originalFileName;
        if (!fileExtension.isEmpty()) {
            newFileName = newFileName.substring(0, newFileName.lastIndexOf('.')) + fileExtension;
        }
        String key = "videos/" + newFileName;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(multipartFile.getBytes(), key, upToken);
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
        } catch (QiniuException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "http://swjs28eb6.hd-bkt.clouddn.com/" + key;
    }



}
